import request from "@/request";
import { setStore, getStore } from "@/utils/storage";

export const getOssToken = () => {
  return request({
    url: "/oss/sign",
    method: "post",
  });
};

export default {
  async getDictList(dictCode) {
    let cache = getStore(dictCode);
    if (!cache) {
      const data = await request({
        url: `/system/dict/data/type/${dictCode}`,
        method: "get",
      });
      setStore({
        name: dictCode,
        content: data,
        cacheTime: 1 * 60 * 1000, // 1分钟
      });
    }
    cache = getStore(dictCode);
    return cache;
  },

  register(data) {
    return request({
      url: "/register",
      method: "post",
      data,
    });
  },
  login(data) {
    return request({
      url: "/login",
      method: "post",
      data,
    });
  },
  getUserInfo() {
    return request({
      url: "/getInfo",
      method: "get",
    });
  },
  updateUserInfo(data) {
    return request({
      url: "/system/user/profile",
      method: "put",
      data,
    });
  },
  updateUserAvatar(data) {
    return request({
      url: "/system/user/profile/avatar",
      method: "post",
      data,
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  getConfigList(params) {
    return request({
      url: "/info/link/list",
      method: "get",
      params,
    });
  },

  toggleSubscribe(data) {
    return request({
      url: "/info/subscribe",
      method: "post",
      data,
    });
  },

  getCodeByFindPwd() {
    return request({
      url: "/captchaImageOfForgetPwd",
      method: "get",
    });
  },
  // 忘记密码-获取二维码
  sendCodeMsg(data) {
    return request({
      url: "/forgetSms",
      method: "post",
      data,
    });
  },
  // 忘记密码-校验二维码
  validCodeMsg(data) {
    return request({
      url: "/checkSmsCode",
      method: "put",
      data,
    });
  },
  resetPassword(data) {
    return request({
      url: "/customerResetPwd",
      method: "put",
      data,
    });
  },
  getUserList(params) {
    return request({
      url: "/system/user/list",
      method: "get",
      params,
    });
  },
  async getProvince() {
    let cache = getStore("province");
    if (!cache) {
      const data = await request({
        url: "/system/config/city",
        method: "get",
      });
      setStore({
        name: "province",
        content: data,
        cacheTime: 24 * 60 * 60 * 1000, // 1天
      });
    }
    cache = getStore("province");
    return cache;
  },
};
